﻿@model RepertorySystem.Models.OrderList
@Scripts.Render("~/bundles/jquery")
@{
    ViewBag.Title = "Create";
}

<h2>出售订单 <span class="glyphicon glyphicon-paperclip" style="color: rgb(69, 63, 63);"></span></h2>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Procurer, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Procurer, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Procurer, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Book_Id, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @{
                    //获取端口
                    IServiceProvider provider = (IServiceProvider)HttpContext.Current;
                    HttpWorkerRequest request = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));
                    var port = HttpContext.Current.Request.Url.Port;

                }
                <script type="text/javascript">
                    $(function () {
                        $.ajax({
                            url:  "http://localhost:"+@port+"/api/Books",
                            type: "get",
                            success: function (result) {
                                $.each(result, function (index, obj) {
                                    $("#book_select").append("<option value="+obj['ID']+">"+obj['Name']+"</option>");
                                });

                            },
                            error: function (msg) {
                                console.log(msg);
                            }

                        });

                    });
                </script>

                <select class="form-control" data-val="true" data-val-number="字段 必须是一个数字。" id="book_select" name="Book_Id" style="width:180px">
                    <option value="-1">请选择书籍</option>
                </select>
                <script type="text/javascript">
                    $("#book_select").blur(function () {
                        
                        var bookId = $("#book_select option:selected").val();
                        $.ajax({
                            url: "http://localhost:" +@port +"/api/StorageListModels/"+bookId,
                            type: "get",
                            success: function (result) {
                                if (result == null) {
                                    alert("库存里没有找到该书籍，请重新选择！");
                                }
                            },
                            error: function (msg) {
                                alert("库存里没有找到该书籍，请重新选择！");
                            }
                        });
                    });
                </script>
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Book_Number, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <input name="Book_Number" class="form-control" id="book_Number1" />
                <script type="text/javascript">
                    $("#book_Number1").focus(function () {
                        var bookId = $("#book_select option:selected").val();
                        var book_Number;
                        //查询书籍的数量
                        $.ajax({
                            url: "http://localhost:" +@port +"/api/StorageListModels/"+bookId,
                            type: "get",
                            success: function (result) {
                                book_Number = result.Book_Number;
                            },
                            error: function (msg) {
                                console.log(msg);
                            }
                        });
                        $("#book_Number1").blur(function () {
                            if ($("#book_Number1").val() > book_Number) {
                                alert("该书库存量最大为：" + book_Number);
                                $("#book_Number1").val("");
                            }
                        });

                    });
                </script>
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Time, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Time, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Time, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <input name="Price" class="form-control" id="book_Price" />

                <script type="text/javascript">
                    $("#book_Price").focus(function () {
                        var book_Id = $("#book_select option:selected").val();
                        var book_Number = $("#book_Number1").val();
                        $.ajax({
                            url:  "http://localhost:"+@port+"/api/Books/"+book_Id,
                            type: "get",
                            success: function (result) {
                                var sumPrice = result.Price * book_Number
                                $("#book_Price").val(sumPrice);
                            },
                            error: function (msg) {
                                console.log(msg);
                            }
                        });
                    })
                </script>
            </div>
        </div>

        <div class="form-group" style="display:none">
            @Html.LabelFor(model => model.State, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <select class="form-control" data-val="true" data-val-number="字段 必须是一个数字。" name="State" style="width:180px">
                    <option value="">请选择状态</option>
                    <option value="0" selected="selected">未完成</option>
                    <option value="1">已完成</option>

                </select>
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-1">
                <input type="submit" value="保存订单" class="btn btn-default" />
            </div>
            <div class="col-md-2">
                @Html.ActionLink("返回出售订单列表", "Index", null, new { @class = "btn btn-default" })
            </div>
        </div>
    </div>
}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
